// master/exam/question.js
var co = require('../../js/common.js')
var _this, app = getApp()
Page({
   data: {
      questions: [],
      i: 0,
      score: 0,
      showScore: 0,
      bgC: [],
      userInfo: ''
   },

   onLoad: function () {
      _this = this
      this.setData({ userInfo: app.aData.userInfo })
   },

   exam_start: function () {
      co.getSelfApi('exam/Question/listAll', {}, function (res) {
         _this.setData({ questions: res.data })
         _this.setOptionBgColor()
         console.log('【考试题目questions】', _this.data, '【res】', res)
      })
   },

   next: function (e) {
      console.log('【点选项e】', e)
      var e_id = e.currentTarget.id
      this.reSetOptionBgColor(e_id)
      var opt_list = this.data.questions[this.data.i].option_list
      if (opt_list[e_id].is_answer == 1) {
         var score = this.data.score + 10
         this.setData({ score: score })
         co.toast('答对，加10分')
      } else {
         co.toast('答错，不加分')
      }
      // setTimeout(this.isRight(), 2000);
      this.isRight()
   },

   isRight: function () {
      var j = _this.data.i + 1
      if (j == _this.data.questions.length) {
         this.updateUserIdentity()
         setTimeout(function () {
            _this.setData({ showScore: 1 })
         }, 1000)
      } else {
         setTimeout(function () {
            _this.setData({ i: j })
            _this.setOptionBgColor()
         }, 1000)
      }
   },

   setOptionBgColor: function () {
      var num = this.data.questions[this.data.i].option_list.length
      var array = []
      for (var i = 0; i < num; i++) {
         array.push('bc_ddd')
      }
      this.setData({ bgC: array })
   },

   reSetOptionBgColor: function (i) {
      var bgc = this.data.bgC
      bgc[i] = 'bc_f60'
      this.setData({ bgC: bgc })
   },

   reExam: function () {
      this.exam_start()
      this.setData({
         i: 0,
         showScore: 0,
         score: 0
      })
      this.setOptionBgColor()
   },

   updateUserIdentity: function () {
      if (this.data.score < 60) {
         return
      }
      var param = {
         unionid: app.aData.userInfo.unionid,
         identity: 'master',
         time2: co.getNowFormatDate(2)
      }
      co.getShareApi('user/mg/updateUserInfo', param, function (res) {
         wx.setStorageSync('userInfo', res.data)
         app.aData.userInfo = res.data
         console.log('【恭喜你成为师傅了】', res)
      })

   },

   toOrderList: function () {
      wx.switchTab({ url: '/home/03list/order' })
   }
})